package com.exam.controller;

import com.exam.entity.*;
import com.exam.service.StudentService;
import com.exam.service.TeacherService;
import com.exam.serviceimpl.LoginServiceImpl;
import com.exam.util.ApiResultHandler;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.util.UUID;


@RestController
public class LoginController {

    @Autowired
    private LoginServiceImpl loginService;
    @Resource
    private TeacherService teacherService;
    @Resource
    private StudentService studentService;

    @PostMapping("/login")
    public ApiResult login(@RequestBody Login login, HttpSession session) {

        String username = login.getUsername();
        String password = login.getPassword();
        Admin adminRes = loginService.adminLogin(username, password);
        if (adminRes != null) {
            session.setAttribute("cid", adminRes.getAdminId());
            return ApiResultHandler.buildApiResult(200, "请求成功", adminRes);
        }

        Teacher teacherRes = loginService.teacherLogin(username, password);
        if (teacherRes != null) {
            session.setAttribute("cid", teacherRes.getTeacherId());

            return ApiResultHandler.buildApiResult(200, "请求成功", teacherRes);
        }

        Student studentRes = loginService.studentLogin(username, password);
        if (studentRes != null) {
            session.setAttribute("cid", studentRes.getStudentId());

            return ApiResultHandler.buildApiResult(200, "请求成功", studentRes);
        }

        return ApiResultHandler.buildApiResult(400, "未审核通过或账号密码错误", null);
    }

    @PostMapping("register")
    public ApiResult register(@RequestBody Login login) {
        String role = login.getRole();
        int row = 0;
        if ("1".equals(role)) {
            Teacher teacher = new Teacher();
            BeanUtils.copyProperties(login, teacher);
            teacher.setTeacherName(login.getRealName());
            teacher.setAccountState("0");
            row = teacherService.add(teacher);
        } else {
            Student student = new Student();
            BeanUtils.copyProperties(login, student);
            student.setStudentName(login.getRealName());
            row = studentService.add(student);
        }
        if (row > 0) {
            return ApiResultHandler.buildApiResult(200, "注册成功");

        } else {
            return ApiResultHandler.buildApiResult(400, "注册失败");

        }
    }

    @PostMapping("/upload/qualification")
    public ApiResult img(MultipartFile file) {
        try {
            if (file.isEmpty()) {
                //return Result.failed("上传文件不能为空");
                return null;
            }
            String originalFilename = file.getOriginalFilename();
            String filename = UUID.randomUUID().toString();
            String filetype = originalFilename.split("\\.")[1];

            String targetFileName = filename + "." + filetype;
            File dest = new File(new File("D:\\common\\mental").getAbsolutePath() + "/" + targetFileName);
            if (!dest.getParentFile().exists()) {
                dest.getParentFile().mkdirs();
            }
            file.transferTo(dest);
            String url = "/profile/" + targetFileName;
            return new ApiResult(200, "上传成功", url);
        } catch (Exception e) {
            e.printStackTrace();
            //return Result.failed(e.getMessage());
            return new ApiResult(400, "上传失败", "");
        }
    }

    @GetMapping("checkTel")
    public Boolean checkTel(String tel) {
        int num = studentService.checkTel(tel,"user");
        int num1 = studentService.checkTel(tel,"admin");
        int num3 = studentService.checkTel(tel,"consultant");
        if((num+num1+num3)>0){
            return false;
        }else{
            return true;
        }
    }


}
